<template>
  <el-container class="outter-wrapper">
    <el-card class="box-card" >
      <div slot="header" class="clearfix el-card__header" v-bind:style="el_header_style">
        <span class="search-title">成&nbsp;果&nbsp;检&nbsp;索</span>
        <el-button style="float: right; padding: 3px 0" type="success" icon="el-icon-refresh" @click="refreshView">视图刷新</el-button>
      </div>
      <div>
        <!--搜索框-->
        <el-row class="search-form">
          <el-form ref="form" :model="form" label-width="80px" :rules="rules">
            <el-row>
              <el-col :span="6">
                <el-tag type="info">条件设定</el-tag>
                <el-tag type="warning">可选参数</el-tag>
                <el-tag type="success">信息检索</el-tag>
                <el-tag type="danger">文件导出</el-tag>
                <el-tag>记录删除</el-tag>
              </el-col>
              <el-col :span="6">
                <el-form-item label="成果类型" prop="protype">
                  <el-select v-model="form.protype" placeholder="请选择成果类型">
                    <el-option v-for="item in protypes" :key="item.label"  :label="item.label" :value="item.value"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="检索模式" prop="searchmode">
                  <el-select v-model="form.searchmode" placeholder="请选择检索模式">
                    <el-option  v-for="item in searchmodes" :key="item.label" :label="item.label" :value="item.value"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-button round type="primary" @click="execute('form')">成果检索</el-button>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="6">
                <el-form-item label="关键字1" prop="itemkeys">
                  <el-input v-model="form.itemkeys" placeholder="请输入项目关键字,可选参数"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="关键字2" prop="awardkeys">
                  <el-input v-model="form.awardkeys" placeholder="请输入奖项关键字,可选参数"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="关键字3" prop="joinkeys">
                  <el-input v-model="form.joinkeys" placeholder="请输入参与人关键字,可选参数"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-button round type="primary" @click="reset">条件重置</el-button>
              </el-col>
            </el-row>
          </el-form>
        </el-row>
<!--        <hr>-->
        <!--搜索结果-->
        <el-row>
          <el-table
              :data="tableData"
              border
              style="width: 100%">
            <el-table-column
                fixed
                label="序号"
                type="index"
                width="50">
            </el-table-column>
            <el-table-column v-for="item in tableColumn" :key="item.prop"
                             :prop="item.prop" :label="item.label" :width="item.width"
            ></el-table-column>
            <el-table-column
                fixed="right"
                label="操作"
                width="210">
              <template slot-scope="scope">
                <el-button @click="handleClick(scope.row)" type="warning" size="mini">编辑</el-button>
                <el-button @click="handleExport(scope.row)" type="success" size="mini">导出</el-button>
                <el-button @click="deleteRow(scope.row)" type="danger" slot="reference" size="mini">删除</el-button>

              </template>
            </el-table-column>
          </el-table>
          <div class="block page-helper-tool">
            <el-pagination
                layout="total,sizes,prev, pager, next,jumper"
                :total="pageHelper.totalSize"
                :page-count="pageHelper.pageCount"
                :page-sizes="pageHelper.pageSizes"
                :page-size="pageHelper.pageSize"
                :current-page="pageHelper.currentPage"
                :pager-count="7"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
            >
            </el-pagination>
          </div>
        </el-row>
      </div>
    </el-card>
    <EditPopOver class="info-popOver" v-show="this.$store.state.isShowPopOver" v-bind:trans-info="rowData"/>
  </el-container>
</template>

<script>
import EditPopOver from '@/components/pop/EditPopOver'
import * as IndexService from '@/service/index/indexService'
import * as UploadService from '@/service/upload/uploadService'
import * as UUIDUtil from '@/service/utils/UUIDUtil'
import * as ColumnTemplate from '@/service/utils/tableColumnTemplate'
import fileDownload from 'js-file-download'
export default {
  components:{
    EditPopOver,
  },
  name: "SearchCenter",
  data(){
    return {
      el_header_style:{
        backgroundImage: "url("+require('@/assets/bk-texture.png')+")",
      },
      //成果类型
      protypes:[
        {
          label:"学生奖项",
          value:"学生奖项"
        },
        {
          label:"教学项目",
          value:"教学项目"
        },
        {
          label:"教学竞赛",
          value:"教学竞赛"
        },
        {
          label:"教改项目",
          value:"教改项目"
        },
        {
          label:"教学论文",
          value:"教学论文"
        },
        {
          label:"科研项目",
          value:"科研项目"
        },
        {
          label:"科研论文",
          value:"科研论文"
        },
        {
          label:"科研奖项",
          value:"科研奖项"
        },
        {
          label:"出版教材",
          value:"出版教材"
        },
        {
          label:"出版专著",
          value:"出版专著"
        },
        {
          label:"专利发表",
          value:"专利发表"
        },
        {
          label:"软著申请",
          value:"软著申请"
        },
      ],
      //检索模式
      searchmodes:[
        {
          label:"AND",
          value:"1",
        },
        {
          label:"NOT",
          value:"2",
        },
      ],
      //表单数据
      form:{
        email:'',//用户信息
        protype:'',//成果类型
        itemkeys:'',//项目关键字
        joinkeys:'',//参与人关键字
        awardkeys:'',//获奖关键字
        searchmode:'',//检索模式
      },
      //表单校验规则
      rules:{
        protype: [{required:true,message:"请选择成果类型",trigger:'blur'}],
        itemkeys: [{required:false,message:"请输入项目关键字",trigger:'blur'}],
        joinkeys: [{required:false,message:"请输入参与人关键字",trigger:'blur'}],
        awardkeys:[{required:false,message:"请输入奖项关键字",trigger:'blur'}],
        searchmode: [{required:true,message:"请选择检索模式",trigger:'blur'}],
      },
      //分页器
      pageHelper:{
        pageSizes:[5,10,15,20],//每页条数选择
        totalSize:0,//总条数
        pageCount:0,//总页数
        pageSize:5,//每页条数(默认10条)
        currentPage:1,//当前页
      },
      //表格列名
      tableColumn:[],
      //表格数据
      tableData: [],
      rowData:undefined,//向子组件传递的数据
    }
  },
  mounted() {
    //获取用户信息
    let email = localStorage.getItem("email");
    this.form.email=email;
    //默认检索模式-AND
    this.form.searchmode="1";
  },
  computed:{
    onUpdateInfo(){
      return this.$store.state.updateInfo;
    }
  },
  watch:{
    onUpdateInfo(val){
      console.log("this.$store.state.updateInfo="+val)
      //发送请求-刷新视图
      this.executeSearchAction({
        //检索条件
        email:this.form.email,
        pageSize:this.pageHelper.pageSize,
        pageNum:this.pageHelper.currentPage,
        joinerkey:this.form.joinkeys,
        itemnamekey:this.form.itemkeys,
        awardkeys:this.form.awardkeys,
        mode:this.form.searchmode,
      })
    },
  },
  methods:{
    //视图刷新动作
    refreshView(){
      //发送请求-刷新视图
      this.executeSearchAction({
        //检索条件
        email:this.form.email,
        pageSize:this.pageHelper.pageSize,
        pageNum:this.pageHelper.currentPage,
        joinerkey:this.form.joinkeys,
        itemnamekey:this.form.itemkeys,
        awardkeys:this.form.awardkeys,
        mode:this.form.searchmode,
      })
    },
    //分页pageSize改变时触发
    handleSizeChange(val){
      this.pageHelper.pageSize=val
      //发送请求-刷新视图
      this.executeSearchAction({
        //检索条件
        email:this.form.email,
        pageSize:this.pageHelper.pageSize,
        pageNum:this.pageHelper.currentPage,
        joinerkey:this.form.joinkeys,
        itemnamekey:this.form.itemkeys,
        awardkeys:this.form.awardkeys,
        mode:this.form.searchmode,
      })
    },
    //修改当前页currentPage时触发
    handleCurrentChange(val){
      this.pageHelper.currentPage=val;
      //发送请求-刷新视图
      this.executeSearchAction({
        //检索条件
        email:this.form.email,
        pageSize:this.pageHelper.pageSize,
        pageNum:this.pageHelper.currentPage,
        joinerkey:this.form.joinkeys,
        itemnamekey:this.form.itemkeys,
        awardkeys:this.form.awardkeys,
        mode:this.form.searchmode,
      })
    },
    //检索条件重置
    reset(){
      this.form.protype=''
      this.form.itemkeys=''
      this.form.joinkeys=''
      this.form.searchmode=''
      this.form.awardkeys=''
    },
    //执行条件检索--此处无需考虑分页条件
    execute(form){
      //表单验证
        this.$refs[form].validate(vaild=>{
          if (vaild){
            //校验通过
            //判断检索类型
            this.executeSearchAction({
              //检索条件
              email:this.form.email,
              pageSize:this.pageHelper.pageSize,
              pageNum:this.pageHelper.currentPage,
              joinerkey:this.form.joinkeys,
              itemnamekey:this.form.itemkeys,
              awardkeys:this.form.awardkeys,
              mode:this.form.searchmode,
            })
          }else {
            //校验失败
             this.$message.error("检索条件不完整!")
          }
        });
    },
    //执行数据检索动作
    executeSearchAction(searchObj){
      switch (this.form.protype){
        case "学生奖项":{
          this.itemSearchTemplate(IndexService.searchStuAwardInfo,searchObj,ColumnTemplate.default.stuAwardTemplate);
          break;
        }
        case "教学项目":{
          this.itemSearchTemplate(IndexService.searchTeacItemInfo,searchObj,ColumnTemplate.default.teacItemTemplate)
          break;
        }
        case "教学竞赛":{
          this.itemSearchTemplate(IndexService.searchTeacContestInfo,searchObj,ColumnTemplate.default.teacTestItemTemplate)
          break;
        }
        case "教改项目":{
          this.itemSearchTemplate(IndexService.searchEduformItemInfo,searchObj,ColumnTemplate.default.eduReformItemTemplate)
          break;
        }
        case "教学论文":{
          this.itemSearchTemplate(IndexService.searchEduReformPaperInfo,searchObj,ColumnTemplate.default.PaperTemplate)
          break;
        }
        case "科研项目":{
          this.itemSearchTemplate(IndexService.searchSciSerrchItemInfo,searchObj,ColumnTemplate.default.sciSearchItemInfoTemplate)
          break;
        }
        case "科研论文":{
          this.itemSearchTemplate(IndexService.searchSciresearchPaperRecord,searchObj,ColumnTemplate.default.PaperTemplate)
          break;
        }
        case "科研奖项":{
          this.itemSearchTemplate(IndexService.searchSciSerrchAwardInfo,searchObj,ColumnTemplate.default.sciSearchAwardInfoTemplate)
          break;
        }
        case "出版教材":{
          this.itemSearchTemplate(IndexService.searchBookInfo,searchObj,ColumnTemplate.default.bookInfoTemplate)
          break;
        }
        case "出版专著":{
          this.itemSearchTemplate(IndexService.searchTreatiseInfo,searchObj,ColumnTemplate.default.treatiseInfoTemplate)
          break;
        }
        case "专利发表":{
          this.itemSearchTemplate(IndexService.searchPatentInfo,searchObj,ColumnTemplate.default.patentInfoTemplate)
          break;
        }
        case "软著申请":{
          this.itemSearchTemplate(IndexService.searchSoftWorkInfo,searchObj,ColumnTemplate.default.softWorkInfoTemplate)
          break;
        }
      }

    },
    //表格数据编辑
    handleClick(row) {
      console.log(row);
      //跳转到新页面-控制菜单选项
      this.rowData={
        type:this.form.protype,//数据类型
        data:row,//数据值
      };//子组件传值
      this.$store.commit("setIsShowPopOver",true);//显示子组件
      // let newPage = this.$router.resolve({path:"/index"});
      // window.open(newPage.href,"_blank");
    },
    //导出文件---通用方法
    handleExport(row){
      if(row.evidence){
        //文件下载操作
        // fileDownload
        UploadService.downloadFile(this.form.email,row.evidence).then(res=>{
          fileDownload(res.data,UUIDUtil.uuid()+".zip");
          this.$message.success("正在导出文件!")
        }).catch(err=>{
          this.$message.error("文件导出失败!")
        })
      }else if(row.itemPartner&&row.itemPartner.evidence){
        UploadService.downloadFile(this.form.email,row.itemPartner.evidence).then(res=>{
          fileDownload(res.data,UUIDUtil.uuid()+".zip");
          this.$message.success("正在导出文件!")
        }).catch(err=>{
          this.$message.error("文件导出失败!")
        })
      }else if(row.author&&row.author.evidence){
        UploadService.downloadFile(this.form.email,row.author.evidence).then(res=>{
          fileDownload(res.data,UUIDUtil.uuid()+".zip");
          this.$message.success("正在导出文件!")
        }).catch(err=>{
          this.$message.error("文件导出失败!")
        })
      }else if(row.sciresearchAwardPartner&&row.sciresearchAwardPartner.evidence){
        UploadService.downloadFile(this.form.email,row.sciresearchAwardPartner.evidence).then(res=>{
          fileDownload(res.data,UUIDUtil.uuid()+".zip");
          this.$message.success("正在导出文件!")
        }).catch(err=>{
          this.$message.error("文件导出失败!")
        })
      }else {
        this.$message.warning("无可下载文件!")
      }
    },
    //删除一行
    deleteRow(row){
      console.log(row)
      console.log(this.form.protype)
      //提示对话框
      let resVal = confirm("确认删除吗?");
      if (resVal){
        //判断检索类型
        switch (this.form.protype){
          case "学生奖项":{
            this.itemDeleteTemplate(IndexService.deleteStuAwardInfo,{
              //指定参数
              email: this.form.email,
              id:row.id,
            },row);
            break;
          }
          case "教学项目":{
            this.itemDeleteTemplate(IndexService.deleteTeacItemInfo,{
              //指定参数
              email: this.form.email,
              id:row.id,
            },row)
            break;
          }
          case "教学竞赛":{
            this.itemDeleteTemplate(IndexService.deleteTeacContentInfo,{
              //指定参数
              email: this.form.email,
              id:row.id,
            },row)
            break;
          }
          case "教改项目":{
            this.itemDeleteTemplate(IndexService.deleteEduformItemInfo,{
              //指定参数
              email: this.form.email,
              id:row.itemPartner.id,
            },row)
            break;
          }
          case "教学论文":{
            this.itemDeleteTemplate(IndexService.deleteEduReformPaperInfo,{
              //指定参数
              email: this.form.email,
              id:row.author.id,
            },row)
            break;
          }
          case "科研项目":{
            this.itemDeleteTemplate(IndexService.deleteSciSerrchItemInfo,{
              //指定参数
              email: this.form.email,
              id:row.itemPartner.id,
            },row)
            break;
          }
          case "科研论文":{
            this.itemDeleteTemplate(IndexService.deleteSciresearchPaperRecord,{
              //指定参数
              email: this.form.email,
              id:row.author.id,
            },row)
            break;
          }
          case "科研奖项":{
            this.itemDeleteTemplate(IndexService.deleteSciSerrchAwardInfo,{
              //指定参数
              email: this.form.email,
              id:row.sciresearchAwardPartner.id,
            },row)
            break;
          }
          case "出版教材":{
            this.itemDeleteTemplate(IndexService.deleteBookInfo,{
              //指定参数
              email: this.form.email,
              id:row.author.id,
            },row)
            break;
          }
          case "出版专著":{
            this.itemDeleteTemplate(IndexService.deleteTreatiseInfo,{
              //指定参数
              email: this.form.email,
              id:row.author.id,
            },row)
            break;
          }
          case "专利发表":{
            this.itemDeleteTemplate(IndexService.deletePatentInfo,{
              //指定参数
              email: this.form.email,
              id:row.author.id,
            },row)
            break;
          }
          case "软著申请":{
            this.itemDeleteTemplate(IndexService.deleteSoftWorkInfo,{
              //指定参数
              email: this.form.email,
              id:row.author.id,
            },row)
            break;
          }
        }
      }else {
        this.$message.info("已取消!");
        return;
      }
    },
    //信息检索模板方法
    itemSearchTemplate(searchMethod,searchObj,tableColumn){
      searchMethod(searchObj).then(res=>{
        //检索结果处理
        this.searchResResolveTemplate(res,tableColumn)
      }).catch(err=>{
        this.$message.error("检索失败,请稍后再试!")
      })
    },
    //信息检索结果处理方法
    searchResResolveTemplate(res,tableColumn){
      //status：状态值判断，-1：邮箱账户为空；0-系统异常；1-正常检索
      switch (res.data.status){
        case -1:{
          this.$message.error("检索参数不完整,请重新指定!");
          break;
        }
        case 0:{
          this.$message.warning("系统异常,请稍后再试!");
          break;
        }
        case 1:{
          //检索成功
          this.$message.success("检索成果,数据已加载...")
          //处理结果数据
          this.pageHelper.currentPage=res.data.pageNum;//当前页数
          this.pageHelper.pageSize=res.data.pageSize;//分页条数
          this.pageHelper.pageCount=res.data.totalPages;//总页数
          this.pageHelper.totalSize=res.data.totalSize;//总条数
          //制空原有数据
          this.tableColumn=[]
          this.tableData=[]
          //构造表格列名
          this.tableColumn=tableColumn
          //替换表格数据
          this.tableData=res.data.data
          break;
        }
      }
    },
    //记录删除模板方法
    itemDeleteTemplate(deleteMethod,searchObj,row){
      //删除行
      deleteMethod(searchObj).then(res=>{
        //判断是否删除成功
        console.log(res)
        if (res.data&&res.data>0){
          //执行界面数据更新操作
          let index=this.tableData.indexOf(row);
          if(index>=0){
            this.tableData.splice(index,1);
            this.$message.success("已删除!");
          }else {
            this.$message.warning("操作异常,请稍后重试!")
          }
        }else {
          this.$message.error("删除失败!")
          return false;
        }
      }).catch(err=>{
        this.$message.error("删除失败!")
        return false;
      })
    }
  }
}
</script>

<style scoped>
.outter-wrapper{
  padding: 10px;
  padding-top: 30px;
  border: 1px solid #ccc;
}
.search-title{
  font-size: 20px;
  font-weight: 700;
  color: #000000;
}
.text {
  font-size: 14px;
}

.item {
  margin-bottom: 18px;
}


.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}
.clearfix:after {
  clear: both
}

.box-card {
  margin: 0 auto;
  /*width: 480px;*/
  width: 95%;
}

.box-card:hover {
  /* width: 480px; */
  box-shadow: 0 5px 15px 0 rgb(0 0 0 / 30%);
}

.el-card__header{
  /*background: linear-gradient(109.6deg, rgb(204, 228, 247) 11.2%, rgb(237, 246, 250) 100.2%);*/
  /*background: #74ebd5;  !* fallback for old browsers *!      background: -webkit-linear-gradient(to right, rgb(116, 235, 213), rgb(172, 182, 229));  !* Chrome 10-25, Safari 5.1-6 *!      background: linear-gradient(to right, rgb(116, 235, 213), rgb(172, 182, 229)); !* W3C, IE 10+/ Edge, Firefox 16+, Chrome 26+, Opera 12+, Safari 7+ *!*/
}
/*检索框样式*/
.search-form{
  border-bottom: 1px solid #ccc;
}
/*分页器*/
.page-helper-tool{
  border-top: 1px solid #ccc;
}
</style>